package com.zhang.zhangapp;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhang.zhangapp.application.MApplication;
import com.zhang.zhangapp.base.http.MCallback;
import com.zhang.zhangapp.bean.location.City;
import com.zhang.zhangapp.bean.location.CityDao;
import com.zhang.zhangapp.http.CommonHttp;
import com.zhang.zhangapp.utils.SPUtils;
import com.zhang.zhangapp.utils.T;

import org.xutils.common.Callback;
import org.xutils.view.annotation.ContentView;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/7/1.
 */
@ContentView(R.layout.activity_splash)
public class SplashActivity extends Activity {

    //    @ViewInject(R.id.splash_gif)
//    GifView gifView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        getCityDatas();
        checkIsFirstOpenApp();
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        } else {
            MApplication.getInstance().initLocation();
        }

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        } else {
            MApplication.getInstance().initDB();
            // 有权限了，去放肆吧。
        }
//
//        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
//        } else {
//            MApplication.getInstance().initDB();
//            // 有权限了，去放肆吧。
//        }
    }


    private void goMain() {
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        SplashActivity.this.finish();
    }

    private void checkIsFirstOpenApp() {
        if ((Boolean) SPUtils.get(this, SPUtils.IS_FIRST_OPEN_APP, true)) {
            Intent intent = new Intent(this, GuideActivity.class);
            startActivity(intent);
            SPUtils.put(this, SPUtils.IS_FIRST_OPEN_APP, false);
        } else {
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    goMain();
                }
            }, 2900); //2900 for release

        }
    }



    /**
     * 获取城市
     */
    private void getCityDatas() {
        CommonHttp.commonHttp("hzg/mainData/areaData", null, getCityDatasCallback);
    }

    /**
     * 获取城市数据
     */
    MCallback getCityDatasCallback = new MCallback() {
        @Override
        public void onSuccess(String data) {
            try {
                Gson gson = new Gson();
                List<City> cityList = gson.fromJson(data, new TypeToken<List<City>>() {
                }.getType());
                if (cityList != null) {
                    CityDao.saveAll(cityList);
                }
            } catch (Exception e) {
            }
        }

        @Override
        public void onSuccess(String message, String code) {
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {
        }

        @Override
        public void onCancelled(Callback.CancelledException cex) {
        }

        @Override
        public void onFinished() {
        }
    };

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.zoomout);
    }
}
